Skip to main content

Last Update: 2025/3/26

OpenAI Completion API

The OpenAI Completion API allows you to generate text completions using OpenAI's language models. This document provides an overview of the API endpoints, request parameters, and response structure.

Endpoint

POST https://platform.llmprovider.ai/v1/completions

Request Headers

HeaderValue
AuthorizationBearer YOUR_API_KEY
Content-Typeapplication/json

Request Body

The request body should be a JSON object with the following parameters:

ParameterTypeDescription
modelstringThe model to use (e.g., gpt-3.5-turbo-instruct).
promptstringThe prompt to generate completions for.
max_tokensinteger(Optional) The maximum number of tokens that can be generated in the completion.
temperaturenumber(Optional) Sampling temperature, between 0 and 2.
top_pnumber(Optional) Nucleus sampling probability, between 0 and 1.
ninteger(Optional) Number of completions to generate for each prompt.
stoparray(Optional) Up to 4 sequences where the API will stop generating further tokens.
presence_penaltynumber(Optional) Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far.
frequency_penaltynumber(Optional) Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far.

Example Request

{
"model": "gpt-3.5-turbo-instruct",
"prompt": "Say this is a test",
"max_tokens": 7,
"temperature": 0
}

Response Body

The response body will be a JSON object containing the generated completions and other metadata.

FieldTypeDescription
idstringUnique identifier for the completion.
objectstringThe type of object returned, usually text_completion.
createdintegerTimestamp of when the completion was created.
modelstringThe model used for the completion.
choicesarrayA list of generated completion choices.
choices[].finish_reasonstringThe reason why the completion ended (e.g., stop, length).
choices[].indexintegerThe index of the choice in the returned list.
choices[].logprobsobject(Optional) Log probabilities of the tokens in the completion.
choices[].textstringThe generated text for the completion.
usageobjectToken usage statistics for the request.

Example Response

{
"id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7",
"object": "text_completion",
"created": 1589478378,
"model": "gpt-3.5-turbo-instruct",
"choices": [
{
"text": "\n\nThis is indeed a test",
"index": 0,
"logprobs": null,
"finish_reason": "length"
}
],
"usage": {
"prompt_tokens": 5,
"completion_tokens": 7,
"total_tokens": 12
}
}

Example Requests

curl -X POST https://platform.llmprovider.ai/v1/completions \
-H "Authorization: Bearer $YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-3.5-turbo-instruct",
"prompt": "Hello, world!",
"max_tokens": 50
}'

For more details, refer to the OpenAI API documentation.